#! /usr/bin/env python
# encoding: utf-8
import os

def build(bld):
    bld.env.CFLAGS_TEST = ["-m32", "-nostartfiles", "-nostdlib", "-fno-builtin", 
                           "-fno-asynchronous-unwind-tables", "-fPIC"]

    bld.env.LINKFLAGS_TEST = ["-m32", "-T../s2l/bld/"+bld.env.TARGET_ARCH_NAME+"/script.ld", 
                              "-Wl,-z,norelro,-z,now,-E,-N,--build-id=none", 
                              "-nostartfiles", "-nostdlib", "-fno-builtin", 
                              "-fno-asynchronous-unwind-tables", "-fPIC"]
    bld.env.CFLAGS_LDL = ["-m32", "-ldl"]
    bld.env.LINKFLAGS_LDL = ["-m32","-ldl"]


    bld.env.SO2BIN_ARGS = ["-a"+bld.env.TARGET_ARCH_NAME,
                           "-llibm.so.6", 
                           "-llibc.so.6"]
    
    bld.program(source="load_code_and_run.c", target="load_code_and_run", use='LDL')


    loader = "../../s2l/rtl/generic/loader.c"
    envelopeC = "../../s2l/rtl/generic/envelope.c"
    
    for f in bld.path.ant_glob("*.test.c"):
        bld(features="c cshlib", source=(f, loader, envelopeC),
                target=f.change_ext('.so'), use='TEST')

        bld(features="so2bin", source = f.change_ext('.so'), 
            target=f.change_ext(".bin"), use='SO2BIN_ARGS')
